package frontend.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JOptionPane;

import backend.model.User;
import backend.service.ServiceException;
import backend.service.UserService;
import backend.service.UserServiceImpl;
import backend.util.ConnectionType;
import frontend.model.TableModel;
import frontend.util.InputValidator;
import frontend.view.InsertFrame;
import frontend.view.InsertPanel;
import frontend.view.MainFrame;

public class InsertFrameController extends WindowAdapter implements
		ActionListener {

	private InsertFrame insFrame;
	private MainFrame mainFrame;
	private UserService userService;
	private ConnectionType ct;
	private Integer id;

	public InsertFrameController(User user, MainFrame mainFrame) {
		ct = ConnectionType.HIBERNATE;
		this.mainFrame = mainFrame;
		this.insFrame = InsertFrame.getInstance();
		this.userService = new UserServiceImpl(ct);
		insFrame.addWindowListener(this);
		this.id = user.getUserId();
		if (user != null) {
			InsertPanel panel = insFrame.getInsPanel();
			panel.getNameField().setText(user.getName());
			panel.getFirstNameField().setText(user.getFirstName());
			panel.getPasswordField().setText(user.getPassword());
			panel.getMobileNrField().setText(user.getMobileNr());
			panel.getEmailField().setText(user.getEmail());
			panel.getFunctionField().setText(user.getFunction());
			panel.getUsernameField().setText(user.getUsername());
		}
		insFrame.getSave().addActionListener(this);
		insFrame.getCancel().addActionListener(this);

	}

	public void save() {
		InsertPanel panel = insFrame.getInsPanel();
		String message = InputValidator.validInput(panel.getNameField()
				.getText(), panel.getFirstNameField().getText(), panel
				.getMobileNrField().getText(), panel.getEmailField().getText());
		if (message.equals("")) {
			User user = new User(panel.getNameField().getText(), panel
					.getFirstNameField().getText(), panel.getMobileNrField()
					.getText(), panel.getEmailField().getText(), panel
					.getFunctionField().getText(), panel.getUsernameField()
					.getText(), panel.getPasswordField().getText(), panel
					.getRoleField().getSelectedItem().toString());
			if (!MainFrameController.update) {
				try {
					userService.insertUser(user);
					TableModel.updateTable(mainFrame, ct);
					insFrame.resetFields();
				} catch (ServiceException f) {
					JOptionPane.showMessageDialog(mainFrame, f.getMessage());
				}

			} else {
				try {
					user.setUserId(id);
					userService.updateUser(user);
					TableModel.updateTable(mainFrame, ct);
					MainFrameController.update = false;
					insFrame.dispose();
					InsertFrame.reset();
				} catch (ServiceException f) {
					JOptionPane.showMessageDialog(mainFrame, f.getMessage());
				}

			}
		} else {
			JOptionPane.showMessageDialog(insFrame, message);
		}
	}

	public void cancel() {
		insFrame.dispose();
		InsertFrame.reset();
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == insFrame.getSave()) {
			InsertPanel panel = insFrame.getInsPanel();
			String message = InputValidator.validInput(panel.getNameField()
					.getText(), panel.getFirstNameField().getText(), panel
					.getMobileNrField().getText(), panel.getEmailField()
					.getText());
			if (message.equals("")) {
				User user = new User(panel.getNameField().getText(), panel
						.getFirstNameField().getText(), panel
						.getMobileNrField().getText(), panel.getEmailField()
						.getText(), panel.getFunctionField().getText(), panel
						.getUsernameField().getText(), panel.getPasswordField()
						.getText(), panel.getRoleField().getSelectedItem()
						.toString());
				if (!MainFrameController.update) {
					try {
						userService.insertUser(user);
						TableModel.updateTable(mainFrame, ct);
						insFrame.resetFields();
					} catch (ServiceException f) {
						JOptionPane
								.showMessageDialog(mainFrame, f.getMessage());
					}

				} else {
					try {
						user.setUserId(id);
						userService.updateUser(user);
						TableModel.updateTable(mainFrame, ct);
						MainFrameController.update = false;
						insFrame.dispose();
						InsertFrame.reset();
					} catch (ServiceException f) {
						JOptionPane
								.showMessageDialog(mainFrame, f.getMessage());
					}

				}
			} else {
				JOptionPane.showMessageDialog(insFrame, message);
			}
		} else if (e.getSource() == insFrame.getCancel()) {
			insFrame.dispose();
			InsertFrame.reset();
		}

	}

	public void windowClosing(WindowEvent e) {
		InsertFrame.reset();
	}

}
